//
//  HSDrawerViewController.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/9/27.
//  Copyright © 2016年 黄华强. All rights reserved.
//

import UIKit

class HSDrawerViewController: UIViewController {
    //用于记录滑动方向
    var isRight:Bool?
    //创建左边视图控制器
    let leftViewVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HSLeftViewController")
    //添加标签控制器
    let tabBarVC = HSTabBarController()
    //动画时长
    let animationTime: TimeInterval = 0.6
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置UI
        setupUI()
        //添加滑动手势
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureFunc(gesture:)))
        view.addGestureRecognizer(panGesture)
    }
    //手势的触发方法
    @objc fileprivate func panGestureFunc(gesture:UIPanGestureRecognizer) {
        switch gesture.state {
        case .began:
            
            let keyWindow = UIApplication.shared.keyWindow
            keyWindow?.endEditing(true)
        //先获得第一次的触摸点
        // currentP = gesture.location(in: gesture.view)
        case .changed:
            
            //先获得移动点
            let offset = gesture.translation(in: gesture.view)
            //根据offset.x的大小来判断滑动方向
            if offset.x > 0 {
                isRight = true
            } else {
                isRight = false
            }
            //分别设置tabBarVC和leftViewVC的形变属性
            let offsetTX = tabBarVC.view.transform.tx + offset.x
            let leftOffsetTx = leftViewVC.view.transform.tx + 0.5*offset.x
            //如果超出范围则不能滑动，启动设置好的动画
            if offsetTX >= screenW * 0.842 {
                rightAnimation()
                return
            } else if offsetTX <= 0 {
                leftAnimation()
                return
            }
            //重新设置视图的位置
            leftViewVC.view.transform = CGAffineTransform(translationX: leftOffsetTx, y: 0)
            tabBarVC.view.transform = CGAffineTransform(translationX: offsetTX, y: 0)
            //重置参考点
            gesture.setTranslation(CGPoint(), in: gesture.view)
            
        case .ended:
            // let endP = gesture.location(in: gesture.view)
            //根据最后滑动方向来确定最终位置
            if isRight ?? true {
                rightAnimation()
            } else {
                leftAnimation()
            }
            
        default:
            break
        }
    }
    
    //懒加载遮盖按钮
    lazy var keepOutButton: UIButton = {
        self.keepOutButton = UIButton(frame: UIScreen.main.bounds)
        self.keepOutButton.backgroundColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
        self.keepOutButton.alpha = 0.0
        self.tabBarVC.view.addSubview(self.keepOutButton)
        self.keepOutButton.addTarget(self, action:#selector(self.leftAnimation), for: .touchUpInside)
        return self.keepOutButton
    }()
    //设置UI视图的方法
    fileprivate func setupUI() {
        view.backgroundColor = UIColor.white
        //添加抽屉效果子控制器
        //左边视图
         leftViewVC.view.frame = CGRect(x: 0, y: 0, width: screenW*0.842, height: screenH)
        leftViewVC.view.layer.shadowColor = UIColor.black.cgColor
        leftViewVC.view.layer.shadowOffset = CGSize(width: 3, height: 0)
        leftViewVC.view.layer.shadowOpacity = 0.9
        self.leftViewVC.view.transform = CGAffineTransform(translationX: -screenW*0.421, y: 0)
        //添加控制器到管理控制器
        addChildViewController(leftViewVC)
        //同时添加视图
        view.addSubview(leftViewVC.view)
        
        //获得左边按钮的监听事件
        tabBarVC.leftBtnBlock = {
            //transform.tx 视图改变的x值,根据这个来判断该启用那个动画
            if self.tabBarVC.view.transform.tx == 0 {
                self.rightAnimation()
            } else {
                self.leftAnimation()
            }
        }
        
        addChildViewController(tabBarVC)
        view.addSubview(tabBarVC.view)

    }
    //向右移动的动画
    fileprivate func rightAnimation() {
        //动画 移动视图
        UIView.animate(withDuration: animationTime, animations: {
            self.tabBarVC.view.transform = CGAffineTransform(translationX: 0.842*screenW + 3, y: 0)
            self.leftViewVC.view.transform = CGAffineTransform(translationX: 0, y: 0)
            self.keepOutButton.alpha = 0.4
            })
    }
    //向左移动的动画
    @objc fileprivate func leftAnimation() {
        UIView.animate(withDuration: animationTime, animations: {
            self.tabBarVC.view.transform = CGAffineTransform(translationX: 0, y: 0)
            self.leftViewVC.view.transform = CGAffineTransform(translationX: -screenW*0.421, y: 0)
            self.keepOutButton.alpha = 0.0
            })
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}
